package com.vastcom.tools;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.credential.PasswordService;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import com.vastcom.model.Users;
import com.vastcom.service.RcService;

public class UserRealm extends AuthorizingRealm {
	

	@Autowired
	private RcService rcservice;
	

	
	//权限管理
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principal) {
		 SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
		 //info.addRole("admin");
		return info;
	}

	
	//身份验证
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(
			AuthenticationToken token) throws AuthenticationException {
		PasswordService passwordservice=new org.apache.shiro.authc.credential.DefaultPasswordService();
		
		String username = token.getPrincipal().toString();//获得Action传入的用户名
		Users user=rcservice.getUserByUsername(username);
		
		if(user!=null){
			
			String pwd =user.getPass();
			
//			boolean flag=passwordservice.passwordsMatch(token.getCredentials(), pwd);
//			System.out.println("flag "+flag);
//			Object pw=token.getCredentials();
//			passwordservice.encryptPassword(pw);
	
		
			return new SimpleAuthenticationInfo(username,pwd ,getName());
			
			
			
		}
		else{
			
			System.out.println("用户不存在");
			return null;
		}
		
		
		
		//System.out.println(token);
//		String username = token.getPrincipal().toString();
//       
//      return new SimpleAuthenticationInfo(username, pwd,getName());
	//return new SimpleAccount(username,pwd,getName());
	}


}
